package com.groupseven.happycampus.controller;

import com.groupseven.happycampus.common.pojo.ErrandAddress;
import com.groupseven.happycampus.common.pojo.ErrandOrder;
import com.groupseven.happycampus.common.utils.OssUtils;
import com.groupseven.happycampus.common.vo.LayuiPageVo;
import com.groupseven.happycampus.common.result.R;
import com.groupseven.happycampus.service.ErrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/errand")
public class ErrandController {

    @Autowired
    private ErrandService errandService;

    // 添加常用地址
    @RequestMapping("/addAddress")
    public R addAddress(ErrandAddress address){
        int result = errandService.addAddress(address);
        if (result == 1){
            return R.ok();
        }else {
            return R.error();
        }
    }

    // 删除常用地址
    @RequestMapping("/deleteAddress")
    public R deleteAddress(@RequestParam("id") Integer id){
        int result = errandService.deleteAddress(id);
        if (result == 1){
            return R.ok();
        }else {
            return R.error();
        }
    }

    // 修改常用地址
    @RequestMapping("/updateAddress")
    public R updateAddress( ErrandAddress address){
        int result = errandService.updateAddress(address);
        if (result == 1){
            return R.ok();
        }else {
            return R.error();
        }
    }

    // 常用地址列表(查询)
    @RequestMapping("addressList")
    public LayuiPageVo<ErrandAddress> addressPageData(@RequestParam Map<String,Object> params){
        LayuiPageVo<ErrandAddress> addressLayuiPageVo = errandService.addressPageData(params);
        return addressLayuiPageVo;
    }

    // 添加订单
    @RequestMapping("/addOrder")
    public R addOrder(ErrandOrder order) {
        int result = errandService.addOrder(order);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    // 取消订单
    @RequestMapping("/deleteOrder")
    public R deleteOrder(Integer id){
        int result = errandService.deleteOrder(id);
        if (result == 1){
            return R.ok();
        }else {
            return R.error();
        }
    }

    // 修改订单
    @RequestMapping("/updateOrder")
    public R updateOrder(ErrandOrder order){
        int result = errandService.updateOrder(order);
        if (result == 1){
            return R.ok();
        }else {
            return R.error();
        }
    }

    // 订单列表(查询/历史订单)
    @RequestMapping("/orderList")
    public LayuiPageVo<ErrandOrder> orderPageData(@RequestParam Map<String,Object> params){
        LayuiPageVo<ErrandOrder> list = errandService.orderPageData(params);
        return list;
    }


    // 上传图片
    @PostMapping("/upload")
    public R uploadPic(MultipartFile file) throws IOException {
        if (file != null){
            String originalFilename = file.getOriginalFilename();
            InputStream inputStream = file.getInputStream();
            String url = OssUtils.uploadFile(inputStream, originalFilename);

            return R.ok().data("url",url);
        }
        return R.ok();
    }

    // 订单的批量删除
    @RequestMapping("/deleteErrandsByIds")
    public R deleteErrandsByIds(@RequestParam String ids) {
        int result = errandService.deleteErrandsByIds(ids);
        if (result > 0) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    // 常用地址和订单地址 多表联查
    @RequestMapping("/userAddress")
    public List<ErrandOrder> showAddress(@RequestParam("telephone") String telephone){
        List<ErrandOrder> addresses = errandService.showAddress(telephone);
        return addresses;
    }

}
